package itman.elm.dao.impl;

import itman.elm.dao.OrdersDao;
import itman.elm.po.Business;
import itman.elm.po.Orders;
import itman.elm.utils.CommonUtil;
import itman.elm.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    Connection conn = null;
    PreparedStatement pstm =null;
    ResultSet rs = null;
    @Override
    public int saveOrders(Orders orders) throws Exception {
        int result = 0;
        String sql = "insert into orders values(null,?,?,?,?,?,0)";
        try {
            conn = DBUtil.getConnection();
            //设置返回自增长列值
            pstm = conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
            pstm.setString(1, orders.getUserId());
            pstm.setInt(2, orders.getBusinessId());
            pstm.setString(3, CommonUtil.getCurrentDate());
            pstm.setDouble(4, orders.getOrderTotal());
            pstm.setInt(5, orders.getDaId());
            pstm.executeUpdate();

            //获取自增长列值
            rs = pstm.getGeneratedKeys();

            if (rs.next()){
                result = rs.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close(pstm);
        }
        return result;
    }

    @Override
    public Orders getOrdersById(Integer orderId) throws Exception {
        Orders orders = null;
        StringBuffer sql = new StringBuffer();
        sql.append(" select o.*, ");
        sql.append("        b.businessId bbusinessId, ");
        sql.append("        b.businessName bbusinessName, ");
        sql.append("        b.deliveryPrice bdeliveryPrice ");
        sql.append(" from orders o left join business b on o.businessId=b.businessId ");
        sql.append(" where o.orderId=? ");
        try {
            conn = DBUtil.getConnection();
            pstm = conn.prepareStatement(sql.toString());
            pstm.setInt(1, orderId);

            rs = pstm.executeQuery();
            if (rs.next()){
                orders = new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setOrderState(rs.getInt("orderState"));

                Business business = new Business();
                business.setBusinessId(rs.getInt("bbusinessId"));
                business.setBusinessName(rs.getString("bbusinessName"));
                business.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));

                orders.setBusiness(business);

            }
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pstm);
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersById(String userId) throws Exception {
        List<Orders> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer();
        sql.append(" select o.*, ");
        sql.append("        b.businessId bbusinessId, ");
        sql.append("        b.businessName bbusinessName, ");
        sql.append("        b.deliveryPrice bdeliveryPrice ");
        sql.append(" from orders o left join business b on o.businessId=b.businessId ");
        sql.append(" where o.userId=?");
        try {
            conn = DBUtil.getConnection();
            pstm = conn.prepareStatement(sql.toString());
            pstm.setString(1, userId);

            rs = pstm.executeQuery();
            while (rs.next()){
                Orders o = new Orders();
                o.setOrderId(rs.getInt("orderId"));
                o.setUserId(rs.getString("userId"));
                o.setBusinessId(rs.getInt("businessId"));
                o.setOrderDate(rs.getString("orderDate"));
                o.setOrderTotal(rs.getDouble("orderTotal"));
                o.setDaId(rs.getInt("daId"));

                Business business = new Business();
                business.setBusinessId(rs.getInt("bbusinessId"));
                business.setBusinessName(rs.getString("bbusinessName"));
                business.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));

                o.setBusiness(business);

                list.add(o);
            }
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close(rs,pstm);
        }
        return list;
    }
}
